package com.baizhiedu.rsm.service.impl;

import com.baizhiedu.rsm.dao.DeleteStudentDao;
import com.baizhiedu.rsm.dao.StudentMessageDao;
import com.baizhiedu.rsm.dao.SubjectDao;
import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.StudentMessage;
import com.baizhiedu.rsm.entity.Subject;
import com.baizhiedu.rsm.service.StudentMessageServcie;
import com.baizhiedu.rsm.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@author lby
 */
@Service
public class StudentMessageServcieImpl implements StudentMessageServcie {
    @Autowired
    private StudentMessageDao studentMessageDao;
    @Autowired
    private DeleteStudentDao deleteStudentDao;
    @Autowired
    private SubjectService subjectService;

    @Override
    public List<Subject> addSchedule(Integer studentId, Integer lastId) {
        //得到学生上一次保存的sid
        StudentMessage studentMessage = studentMessageDao.selectByPrimaryKey(studentId);

        Integer frontId = Integer.parseInt(studentMessage.getSchedule()==null?"5":studentMessage.getSchedule());

//        进度更新
        if(lastId == null){
            System.out.println("进度参数为空，不执行添加操作~~~");
        }else {
            //        进度检查 目前的标准是每天必须看至少两个视频
            if(lastId>=frontId && lastId-frontId<2){
//          备注追加进度慢 以及进度信息
                String remark = studentMessage.getRemark();
                remark += "进度慢："+frontId+"-"+lastId;
                studentMessage.setRemark(remark);
            }

            studentMessage.setSchedule(lastId.toString());
            studentMessageDao.updateByPrimaryKey(studentMessage);
        }
//        题目获取 如果lastid比之前的小 如果没有 调用题目获取方法
        return subjectService.showSubjectBySid(frontId,lastId);
    }


    @Override
    public List<StudentMessage> selectStudentByIds(Integer[] id) {
        return null;
    }

    @Override
    public Map insert(StudentMessage studentMessage) {
        Map map = new HashMap(1);
        try{
            studentMessageDao.insert(studentMessage);
            map.put("insert",true);
        }catch (Exception e){
            map.put("insert",false);
        }
        return map;
    }

    @Override
    public List<StudentMessage> showAllStudentByClassId(Integer classId) {
        return studentMessageDao.selectAllStudentByClassId(classId);
    }

    @Override
    public Map updateOne(StudentMessage studentMessage) {
        Map map = new HashMap(1);
        try{
            studentMessageDao.updateByPrimaryKey(studentMessage);
            map.put("update",true);
        }catch (Exception e){
            map.put("update",false);
        }
        return map;
    }

    @Override
    public Map deleteSelect(List<StudentMessage> students) {
        Map map = new HashMap(1);
        try{
            for (StudentMessage s:students) {
                Integer id = s.getId();
                //删除之前 把数据查询出来放入DeleteStudent表
                StudentMessage studentMessage = studentMessageDao.selectByPrimaryKey(id);
                studentMessage.setId(null);
                deleteStudentDao.insert(studentMessage);
                //在原表中删除
                studentMessageDao.deleteByPrimaryKey(id);
            }
            map.put("deleteSelect",true);
        }catch (Exception e){
            map.put("deleteSelect",false);
        }
        return map;
    }

    @Override
    public Student getStudentById(Integer studentid) {
        return null;
    }
}
